package reports.builders;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Collection;

public class HTMLBuilder implements IBuilder {

	private String htmlPage;
	private String filePath;
	
	public HTMLBuilder(String pageTitle)
	{
		htmlPage="";
		filePath=pageTitle;
		init(pageTitle);

	}

	/**
	 * Inits the html file with a doc type and title
	 * opens the html body
	 * @param pageTitle title that gets set
	 */

	private void init(String pageTitle)
	{
		htmlPage+="<!DOCTYPE html> <html> <head><title>";
		htmlPage+=pageTitle+"</title>";
		htmlPage+="<style type=\"text/css\">" +
				"h1{text-align:center;}" +
				"h2{text-align:left;}"+
				"table {text-align:center;margin:auto;}"+
				".tables{text-align:left}"+
				"td{text-align:left}"+
				"p{border:1px solid gray;}"+
								"</style> " +
				"</head> <body>"; // opens body
	}

	/**
	 * Closes the html page by closing the body and the html tags
	 */
	private void close()
	{
		htmlPage+="</body></html>";
	}
	//see interface for function decelerations
	@Override
	public void addTitle(String title)
	{
		htmlPage+="<h1>"+title+"</h1>";

	}

	@Override
	public void startTable(int columns) {
		htmlPage+="<div class=\"tables\">";
		htmlPage+="<table border=\"1\">";

	}

	/**
	 * This function closes the table in HTML, it should all 
	 * be called once all the rows have been added
	 */
	@Override
	public void endTable() 
	{
		
		htmlPage+="</table>";
		htmlPage+="</div>";


	}

	@Override
	public void addRow(Collection<String> row)
	{
		htmlPage+="<tr>";
		for(String element:row)
		{
			htmlPage+="<td>"+element.toString()+"</td>";
		}
		htmlPage+="</tr>";

	}

	@Override
	public void addTableHeader(Collection<String> headers) {
		htmlPage+="<tr>";
		for(String element : headers)
		{
			htmlPage+="<th>"+element+"</th>";
		}
		htmlPage+="</tr>";

	}

	@Override
	public void insertPadding(int paddingValue)
	{
		//htmlPage+="<div>"

	}

	@Override
	public void addsubTitle(String subTitle) {
		htmlPage+="<h3>"+subTitle+"</h3>";

	}

	@Override
	public void addParagraph(String text) {
		htmlPage+="<p>"+text+"</p>";
	}

	@Override
	public void saveToFile()
	{
		close();
		File reportsFolder= new File("reports");
		if(!reportsFolder.exists())
		{
			reportsFolder.mkdir();
		}
		filePath="reports/"+filePath+"-"+System.currentTimeMillis()+".html";
		File file = new File(filePath);
		try
		{
			FileWriter writer = new FileWriter(file);
			writer.write(htmlPage);
			writer.flush();
			writer.close();
		} 
		catch (IOException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		

	}
	
	// tested with this function
//	public static void main(String [] argv)
//	{
//		ArrayList<String> header= new ArrayList<String>();
//		header.add("name");header.add("value"); header.add("other");
//		
//		HTMLBuilder builder = new HTMLBuilder("testPage");
//		builder.addTitle("this is only a test page, this is the title");
//		builder.startTable();
//		builder.addTableHeader(header);
//		for(int x=0;x<4;x++)
//		{
//			ArrayList<String> tmp= new ArrayList<String>();
//			for(int i=0;i<3;i++)
//			{
//				tmp.add(Integer.toString(i*x));
//			}
//			builder.addRow(tmp);
//		}
//		builder.addTable();
//		builder.addsubTitle("this is a subtitle");
//		builder.addText("this is a text box ,\n notification will be in this box\n\nright here" );
//		builder.close();
//		builder.saveToFile();
//		
//		Runtime r = Runtime.getRuntime();
//		try
//		{
//			r.exec("firefox "+builder.getFilePath());
//		
//		} catch (IOException e)
//		{
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//	}
	
	public String getFilePath()
	{

		return filePath;
	}

	@Override
	public void addList(Collection<String> list)
	{
		htmlPage+="<ul>";
		for(String str: list)
		{
			htmlPage+="<li>"+str+"</li>";
		}
		htmlPage+="</ul>";
		
	}





}
